<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author ThinkGem
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'m8:machine:check:sjts'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="70%"
  >
    <template #title>
      <!-- <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span> -->
      检验登记
    </template>
    <BasicForm @register="registerForm"> </BasicForm>
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsTestTestDataForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  // import { TestData, testDataSave, testDataForm } from '/@/api/test/testData';
  import { officeTreeData } from '/@/api/sys/office';
  // import { areaTreeData } from '/@/api/sys/area';

  import { checkForm, checksave, Machine } from '/@/api/m8/machine/machine';
  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('machine.check');
  const { showMessage } = useMessage();
  const record = ref<Machine>({} as Machine);
  // const getTitle = computed(() => ({
  //   icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
  //   value: record.value.isNewRecord ? t('新增数据') : t('编辑数据'),
  // }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('任务'),
      field: 'task.taskCode',
      component: 'Input',
      componentProps: {
        maxlength: 200,
      },
      required: true,
    },
    {
      label: t('检验类型'),
      field: 'checkType',
      component: 'Select',
      componentProps: {
        dictType: 'm8_task_check_type',
        allowClear: true,
      },
    },
    {
      label: t('抽检数量'),
      field: 'checkQty',
      component: 'Input',
      componentProps: {
        maxlength: 10,
      },
      required: true,
    },
    {
      label: t('检验结果'),
      field: 'checkRs',
      component: 'Select',
      componentProps: {
        dictType: 'm8_task_check_result',
        allowClear: true,
      },
    },
    {
      label: '检验人',
      field: 'createBy',
      fieldLabel: 'creator.userName',
      component: 'TreeSelect',
      required: true,
      componentProps: {
        api: officeTreeData,

        params: { isLoadUser: true, userIdPrefix: '', isAll: true },
        canSelectParent: false,
        allowClear: true,
      },
    },
    {
      label: '送检人',
      field: 'jyManagerCodes',
      fieldLabel: 'jyManagerNames',
      component: 'TreeSelect',
      required: true,
      componentProps: {
        api: officeTreeData,
        treeCheckable: true,
        params: { isLoadUser: true, userIdPrefix: '', isAll: true },
        canSelectParent: true,
        allowClear: true,
      },
    },
    {
      label: '报废负责人',
      field: 'bfManagerCodes',
      fieldLabel: 'bfManagerNames',
      component: 'TreeSelect',
      required: true,
      componentProps: {
        api: officeTreeData,
        treeCheckable: true,
        params: { isLoadUser: true, userIdPrefix: '', isAll: true },
        canSelectParent: true,
        allowClear: true,
      },
    },
    {
      label: t('日期时间'),
      field: 'nextCheckDate',
      component: 'DatePicker',
      componentProps: {
        format: 'YYYY-MM-DD HH:mm:ss',
        showTime: { format: 'HH:mm:ss' },
      },
    },
    {
      label: t('检验描述'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      colProps: { lg: 24, md: 24 },
    },
    {
      label: t('图片上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'm8TaskCheck_image',
        uploadType: 'image',
      },
      colProps: { lg: 24, md: 24 },
      // 文件上传的必填验证实例
      // rules: [
      //   { required: true },
      //   {
      //     validator(_rule, value) {
      //       return new Promise((resolve, reject) => {
      //         const len = !value || value['testData_image__len'] || 0;
      //         if (len == 0) reject(t('请上传图片'));
      //         else resolve();
      //       });
      //     },
      //   },
      // ],
    },
    {
      label: t('文件上传'),
      field: 'dataMap',
      component: 'Upload',
      componentProps: {
        loadTime: computed(() => record.value.__t),
        bizKey: computed(() => record.value.id),
        bizType: 'm8TaskCheck_file',
        uploadType: 'all',
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 12, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    // setDrawerProps({ loading: true });
    await resetFields();
    const res = await checkForm(data);
    // record.value = (res.testData || {}) as Machine;
    // record.value.__t = new Date().getTime();
    // setFieldsValue(record.value);
    // setDrawerProps({ loading: false });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await checksave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
